package com.ruoyi.library.service.impl;

import java.util.List;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.library.domain.dto.NexusToolLibraryDTO;
import com.ruoyi.library.domain.vo.NexusToolLibraryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.library.mapper.NexusToolLibraryMapper;
import com.ruoyi.library.domain.NexusToolLibrary;
import com.ruoyi.library.service.INexusToolLibraryService;

/**
 * 资源库Service业务层处理
 *
 * @author Afanda
 * @date 2025-04-29
 */
@Service
public class NexusToolLibraryServiceImpl implements INexusToolLibraryService {
    @Autowired
    private NexusToolLibraryMapper nexusToolLibraryMapper;

    @Autowired
    private RedisCache redisCache;

    private static final String LIBRARY_CACHE_KEY = "library";

    /**
     * 查询资源库
     *
     * @param id 资源库主键
     * @return 资源库
     */
    @Override
    public NexusToolLibrary selectNexusToolLibraryById(Long id) {
        return nexusToolLibraryMapper.selectNexusToolLibraryById(id);
    }

    /**
     * 查询资源库列表
     *
     * @param nexusToolLibrary 资源库
     * @return 资源库
     */
    @Override
    public List<NexusToolLibrary> selectNexusToolLibraryList(NexusToolLibrary nexusToolLibrary) {
        return nexusToolLibraryMapper.selectNexusToolLibraryList(nexusToolLibrary);
    }

    /**
     * 查询资源库列表
     *
     * @param nexusToolLibrary 资源库
     * @return 资源库
     */
    @Override
    public List<NexusToolLibraryVo> selectNexusToolLibraryVoList(NexusToolLibrary nexusToolLibrary) {
        return nexusToolLibraryMapper.selectNexusToolLibraryVoList(nexusToolLibrary);
    }

    /**
     * 新增资源库
     *
     * @param nexusToolLibrary 资源库
     * @return 结果
     */
    @Override
    public int insertNexusToolLibrary(NexusToolLibrary nexusToolLibrary) {
        nexusToolLibrary.setCreateTime(DateUtils.getNowDate());
        return nexusToolLibraryMapper.insertNexusToolLibrary(nexusToolLibrary);
    }

    /**
     * 修改资源库
     *
     * @param nexusToolLibrary 资源库
     * @return 结果
     */
    @Override
    public int updateNexusToolLibrary(NexusToolLibrary nexusToolLibrary) {
        nexusToolLibrary.setUpdateTime(DateUtils.getNowDate());
        return nexusToolLibraryMapper.updateNexusToolLibrary(nexusToolLibrary);
    }

    /**
     * 批量删除资源库
     *
     * @param ids 需要删除的资源库主键
     * @return 结果
     */
    @Override
    public int deleteNexusToolLibraryByIds(Long[] ids) {
        List<NexusToolLibrary> nexusToolLibraryList = nexusToolLibraryMapper.deleteNexusToolLibraryByIdsList(ids);
        for(NexusToolLibrary nexusToolLibrary:nexusToolLibraryList){
            String key = LIBRARY_CACHE_KEY + nexusToolLibrary.getCategoryId();
            redisCache.deleteObject(key);
        }
        return nexusToolLibraryMapper.deleteNexusToolLibraryByIds(ids);
    }

    /**
     * 删除资源库信息
     *
     * @param id 资源库主键
     * @return 结果
     */
    @Override
    public int deleteNexusToolLibraryById(Long id) {
        return nexusToolLibraryMapper.deleteNexusToolLibraryById(id);
    }


    /**
     *
     */
    @Override
    public List<NexusToolLibraryVo> selectNexusToolLibraryVoListByCategoryId(Long id) {
        List<NexusToolLibraryVo> nexusToolLibraryVos = nexusToolLibraryMapper.selectNexusToolLibraryVoListByCategoryId(id);
        if (nexusToolLibraryVos.size() == 0 || nexusToolLibraryVos == null) {
            return null;
        }
        return nexusToolLibraryVos;
    }

    /**
     * 根据导航栏id获取数据
     */
    @Override
    public List<NexusToolLibraryVo> selectNexusToolLibraryVoListByNavId(Long id) {
        List<NexusToolLibraryVo> nexusToolLibraryVos = nexusToolLibraryMapper.selectNexusToolLibraryVoListByNavId(id);
        if (nexusToolLibraryVos.size() == 0 || nexusToolLibraryVos == null) {
            return null;
        }
        return nexusToolLibraryVos;
    }

    /**
     * 搜索资源库
     * @param nexusToolLibraryDTO 资源库DTO
     * @return 资源库VO列表
     */
    @Override
    public List<NexusToolLibraryVo> searchToolLibrary(NexusToolLibraryDTO nexusToolLibraryDTO){
        NexusToolLibrary nexusToolLibrary = new NexusToolLibrary();
        BeanUtils.copyProperties(nexusToolLibraryDTO,nexusToolLibrary);
        System.out.println(nexusToolLibrary);
        List<NexusToolLibraryVo> nexusToolLibraryList = nexusToolLibraryMapper.searchToolLibrary(nexusToolLibrary);
        if (nexusToolLibraryList.size() == 0 || nexusToolLibraryList == null) {
            throw new IllegalArgumentException("搜索资源库失败");
        }
        return nexusToolLibraryList;
    }

}
